##########################################################################################
library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)
library(ggpubr)
library(ggsci)

##########################################################################################

option_list <- list(
    make_option(c("--input_file"), type = "character") ,
    make_option(c("--TMB_raw_file"), type = "character") ,
    make_option(c("--images_path"), type = "character")
)

if(1!=1){
    
    work_dir <- ""
    input_file <- paste(work_dir,"TCGA_info_include_mut.tsv",sep="")
    TMB_raw_file <- paste(work_dir,"TCGA_STAD.TMB.tsv",sep="")
	  images_path <- paste(work_dir,"",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_file <- opt$input_file
TMB_raw_file <- opt$TMB_raw_file
images_path <- opt$images_path

###########################################################################################

info <- data.frame(fread(input_file))
TMB_raw <- data.frame(fread(TMB_raw_file))

###########################################################################################

col <- c(
  brewer.pal(9,"YlGnBu")[6],
  rgb(234,106,79,alpha=255,maxColorValue=255),
  rgb(203,24,30,alpha=255,maxColorValue=255),
  rgb(255,0,0,alpha=255,maxColorValue=255)
  )

names(col) <- c("IM" , "IGC" , "DGC" , "GC")
col <- col[2:3]

###########################################################################################

dat_ccf <- subset( info , Hugo_Symbol=="TP53" )
dat_sample <- TMB_raw
dat_sample$TP53 <- ifelse( dat_sample$bcr_patient_barcode %in% dat_ccf$bcr_patient_barcode , "Mut" , "NoMut"  )

###########################################################################################
## 多个样本负荷取中位数
dat_sample <- dat_sample[,c("bcr_patient_barcode","histological_type","TMB..nonsynonymous.","TP53")]
colnames(dat_sample) <- c("Patient","Class","BurdenExon","TP53")
n <- "Stomach  Adenocarcinoma  Diffuse Type"
dat_sample$Class <- ifelse( dat_sample$Class %in% n , "DGC" , "IGC" )
#dat_plot2 <- dat_sample
dat_plot2 <- dat_sample %>% group_by(Patient,Class,TP53) %>% summarize(BurdenExon = median(BurdenExon))

dat_plot2$Class <- factor( dat_plot2$Class , levels = names(col) , order = T )

###########################################################################################

plotBurden <- function(dat_plot_tmp = dat_plot_tmp){

    my_comparisons_1 <- list(c(1, 2))

plot <- ggplot( dat_plot_tmp , aes( x = TP53 , y = MutBurden_use , color = TP53 ) ) +
        geom_boxplot(alpha =1 , outlier.size=0 , size = 1.5 , width = 0.6) +
        geom_jitter(position = position_jitter(0.17) , size = 1 , alpha = 0.7) +
        facet_grid(.~Class,space='free_x',scales='free_x') +
        scale_color_npg() +
        xlab(NULL) +
        ylab("Mutation rate per MB")+
        theme_bw() +
        stat_compare_means(comparisons = my_comparisons_1) +
        theme(panel.background = element_blank(),#设置背影为白色#清除网格线
            legend.position ='left',
            legend.title = element_blank() ,
            panel.grid.major=element_line(colour=NA),
            plot.title = element_text(size = 8,color="black",face='bold'),
            legend.text = element_text(size = 10,color="black",face='bold'),
            axis.text.y = element_text(size = 10,color="black",face='bold'),
            axis.title.x = element_text(size = 10,color="black",face='bold'),
            axis.title.y = element_text(size = 10,color="black",face='bold'),
            axis.ticks.x = element_blank(),
            axis.text.x = element_text(size = 10,color="black",face='bold') ,
            ) 
        out_name <- paste0( images_path , "/TCGA_mutBurden.TP53" , ".pdf" )  
        ggsave(file=out_name,plot=plot,width=4,height=5)    
}

dat_plot_tmp <- dat_plot2
dat_plot_tmp$MutBurden_use <- dat_plot_tmp$BurdenExon
plotBurden(dat_plot_tmp = dat_plot_tmp )
